#!/usr/bin/env python
#
# Run this program if you want a quick start to developing Bandleader
# on Windows using CMake and MinGW.
#
# The following commands will be available:
# bin\make.bat -- for building
# bin\run.bat -- for running the program
#

import os

config_file_name = 'config.txt'

if not os.path.exists(config_file_name):
    print "Creating sample config file " + config_file_name + "."
    print "Please edit this file for your system setup, then run this again."
    print
    print "If by blind luck you have the same setup as mine,"
    print "then you don't need to run this again."
    print
    config_file = open(config_file_name, 'w')

    config_file.write("""REM
REM Custom configuration
REM
set MINGW_DIR=c:\\qt\\mingw\\bin
set CMAKE_DIR=c:\\qt\\cmake\\bin
set QT_DIR=c:\\qt\\4.2.2\\bin
REM
set PATH=%MINGW_DIR%;%CMAKE_DIR%;%QT_DIR%""")


config_file = open(config_file_name)
config_string = config_file.read()
config_file.close()


print "Creating common.mk"

cmake_dir = ''

for line in config_string.split('\n'):
    if line.startswith("set CMAKE_DIR"):
        cmake_dir = line[len("set CMAKE_DIR="):]

f = open("common.mk", 'w')

f.write("CMAKE=" + cmake_dir + r'\cmake.exe -G "MinGW Makefiles"' + '\n')

f.close()


print "Creating bin\launcher.bat"

#

os.chdir("bin")

f = open("launcher.bat", 'w')

f.write("""
@echo off
REM
REM Sets up the path before launching the program.
REM

""" + \
config_string + \
"""

REM Get executable name
set exename=%1

REM Build arguments from arg2 (arg1 is executable name)
set args=
:loop
set args=%args% %2
shift
if not "%~2"=="" goto loop

REM Run the program
"%exename%" %args%
""")

f.close()
